//
//  TongXunLuViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/6/24.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "TongXunLuViewController.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "PersonInfo.h"
#import "PhoneNameTableViewCell.h"
#import "PeopleDataViewController.h"
#import "HonorManagementViewController.h"
#import "MJRefresh.h"
#import "LMComBoxView.h"
#import "AddFamilyCallViewController.h"
#import "CreateGroupViewController.h"
#import "ChatGroupDetailViewController.h"
#import "TongXunLuTableViewCell.h"
#import "BaseTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "SDImageCache.h"

#import "AddressBookGroup.h"

@interface TongXunLuViewController ()<LMComBoxViewDelegate,UISearchBarDelegate>{
    
    NSMutableArray *tongXunLuArray,*searchArray,*ziMuArray,*sectionArray,*tableViewArray,*searchSection;
    BOOL isSearch;
    NSString *currentZiMu,*currentSearchZiMu,*currentSearchCondition;
    //UITableView *shouZiMuTableView;
    float screenWidth,screenHeight;
    int sectionCount,refreshPage,searchPage,searchSectionCount;
    MBProgressHUD *hudFirst;
    //NSDictionary *tableViewDic;
    UIView *tableHeaderView;
    PersonInfo   *cellPerson;
    NSMutableArray *addressBookArray;
    LMComBoxView *mlmBox;
}

@end

@implementation TongXunLuViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    ziMuArray=[[NSMutableArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z", nil];
    
    screenWidth=[UIScreen mainScreen].bounds.size.width;
    screenHeight=[UIScreen mainScreen].bounds.size.height;
    
    self.title=@"通讯录";
    isSearch=NO;
    
    _searchBar=[[UISearchBar alloc]init];
    _searchBar.frame=CGRectMake(0, 0, screenWidth, 44);
    _searchBar.delegate=self;
    _searchBar.placeholder=@"搜索";
    [self.view addSubview:_searchBar];
    
    self.tongXunLuTableView.rowHeight=50;
    
    [self createIndexButton];
    [self createNavButton];
    [self createSearchContion];
    
    if ((!self.orHonorName)&&(!self.orAddGroupMember)&&(!self.orCreateGroupMember)) {
        
        [self getAddressBookGroup];//获取通讯录分组
        //self.tongXunLuTableView.tableHeaderView=[self tableHeaderView];
    }
    
    if (self.orAddGroupMember||self.orCreateGroupMember) {
        self.title=@"选择联系人";
        _chooseGroupMember=[[NSMutableArray alloc]init];
        self.tongXunLuTableView.editing=YES;
    }
}

//设置搜索条件按钮
-(void)createSearchContion{
    
    self.searchBar.frame=CGRectMake(66, 5, screenWidth-76, 34);
    _searchBar.backgroundImage=[UIImage imageNamed:@"white.png"];
    [_searchBar.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [_searchBar.layer setBorderWidth:1.0];
    _searchBar.layer.cornerRadius = 5;
    _searchBar.clipsToBounds = YES;
    mlmBox = [[LMComBoxView alloc] initWithFrame:CGRectMake(10, 5, 60, 34)];
    mlmBox.backgroundColor = [UIColor whiteColor];
    mlmBox.titlesList = [NSMutableArray arrayWithObjects:@"姓名", @"地区", nil];
    mlmBox.btnBgImgStr = @"searchLeft.png";
    mlmBox.delegate = self;
    mlmBox.supView = self.view;
    [mlmBox defaultSettings];
    
    [self.view addSubview:mlmBox];
}

-(void)selectAtIndex:(int)index inCombox:(LMComBoxView *)_combox
{
    NSLog(@"index:%d, title:%@", index, _combox.titlesList[index]);
    currentSearchCondition=_combox.titlesList[index];
}

//设置导航栏按钮
-(void)createNavButton{
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    if (self.orAddGroupMember||self.orCreateGroupMember) {
        //如果是添加群成员，创建确定按钮
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        rightBtn.frame = CGRectMake(0, 0, 65, 30);
        [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightBtn addTarget:self action:@selector(doneAction) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
        UIBarButtonItem *flexSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
        flexSpacer.width = flexSpacer.width - 20;
        [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:flexSpacer, rightBarItem, nil]];
    }
}
//设置表头本村人和家乡人
- (UIView *)tableHeaderView
{
    if (tableHeaderView == nil) {
        
        tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tongXunLuTableView.frame.size.width, 50*addressBookArray.count)];
        tableHeaderView.backgroundColor=[UIColor whiteColor];
        
        for (int i=0;i<addressBookArray.count; i++) {
            
            AddressBookGroup *addressGroup=[addressBookArray objectAtIndex:i];
            UIImageView *benCunImageView=[[UIImageView alloc]init];
            benCunImageView.frame=CGRectMake(10, 5+i*50, 40, 40);
            benCunImageView.layer.cornerRadius = 20;
            benCunImageView.clipsToBounds = YES;
            benCunImageView.image=[UIImage imageNamed:@"groupchatimg"];
            [tableHeaderView addSubview:benCunImageView];
            
            UILabel *benCunLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 5+i*50, 240, 40)];
            if ([addressGroup.mark isEqualToString:@"bcr"]||[addressGroup.mark isEqualToString:@"jxr"]) {
                benCunLabel.text=[NSString stringWithFormat:@"%@(%@人)",addressGroup.group_name,addressGroup.p_count];
            }else if([addressGroup.mark isEqualToString:@"jxc"]){
                benCunLabel.text=[NSString stringWithFormat:@"家乡  %@(%@人)",addressGroup.group_name,addressGroup.p_count];
            }else if ([addressGroup.mark isEqualToString:@"fwc"]){
                benCunLabel.text=[NSString stringWithFormat:@"服务村  %@(%@人)",addressGroup.group_name,addressGroup.p_count];
            }
            [tableHeaderView addSubview:benCunLabel];
            
            UIButton *benCunButton=[UIButton buttonWithType:UIButtonTypeCustom];
            benCunButton.frame=CGRectMake(5,i*50, screenWidth-10, 49);
            benCunButton.tag=i;
            [benCunButton addTarget:self action:@selector(goToCunLiInfo:) forControlEvents:UIControlEventTouchUpInside];
            [tableHeaderView addSubview:benCunButton];
            
            UIView *sperView=[[UIImageView alloc]initWithFrame:CGRectMake(0,(i+1)*50-1,screenWidth, 1)];
            sperView.backgroundColor=[UIColor colorWithRed:220/255.0 green:220/255.0 blue:223/255.0 alpha:1];
            [tableHeaderView addSubview:sperView];
        }
        
    }
    
    return tableHeaderView;
}
//创建字母索引
-(void)createIndexButton{
    
    float hight=(screenHeight-108)/26;
    
    for (int i=0; i<26; i++) {
        
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(screenWidth-25, 44+i*hight, 25, hight);
        [btn setTitle:[ziMuArray objectAtIndex:i] forState:UIControlStateNormal];
        //[btn setTitleColor:[UIColor colorWithRed:12/255.0 green:105/255.0 blue:255/255.0 alpha:1] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(goToSection:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag=i;
        [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [self.view addSubview:btn];
    }
}

#pragma mark -进入相应村

-(void)goToCunLiInfo:(UIButton *)btn{
    
    if (mlmBox.isOpen) {
        [mlmBox tapAction];
    }
    [self.view endEditing:YES];
    AddressBookGroup *address=[addressBookArray objectAtIndex:btn.tag];
    AddFamilyCallViewController *addVC=[[AddFamilyCallViewController alloc]init];
    addVC.personType=address.group_name;
    addVC.mark=address.mark;
    addVC.areaid=address.areaids;
    addVC.orTongXunLu=YES;
    addVC.orRefresh=YES;
    [self.navigationController pushViewController:addVC animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    if (self.orRefresh) {
        
        sectionCount=-1;
        searchSectionCount=-1;
        //tongXunLuArray=[[NSMutableArray alloc]init];
        
        searchArray=[[NSMutableArray alloc]init];
        sectionArray=[[NSMutableArray alloc]init];
        
        tableViewArray=[[NSMutableArray alloc]init];
        searchSection=[[NSMutableArray alloc]init];
        
        [self getTongXunLuInfo];
        self.orRefresh=NO;
    }
}

//跳转到指定区
-(void)goToSection:(UIButton *)btn{
    
    [self.view endEditing:YES];
    NSString *ziMuString=[ziMuArray objectAtIndex:btn.tag];
    //搜索列表
    if (isSearch) {
        for (int i=0; i<searchSection.count; i++) {
            
            if ([ziMuString isEqualToString:[searchSection objectAtIndex:i]]) {
                
                NSIndexPath*indexPath =[NSIndexPath indexPathForRow:0 inSection:i];
                //单元格滚到指定的位置
                [self.tongXunLuTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
                
                return;
            }
        }
    }else{
        //通讯录列表
        for (int i=0; i<sectionArray.count; i++) {
            
            if ([ziMuString isEqualToString:[sectionArray objectAtIndex:i]]) {
                
                NSIndexPath*indexPath =[NSIndexPath indexPathForRow:0 inSection:i];
                //单元格滚到指定的位置
                [self.tongXunLuTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
                
                return;
            }
        }
        
    }
    
}

//点击search按钮开始搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    [self.view endEditing:YES];
    [self getSearchWorkAreaWithString:searchBar.text];
}

//本地搜索居住地或者搜索姓名添加群成员
-(void)getSearchWorkAreaWithString:(NSString *)str{
    
    NSMutableArray *areaSearchArray=[[NSMutableArray alloc]init];
    
    if (![currentSearchCondition isEqualToString:@"地区"]) {
        
        for (NSMutableArray *arr in tableViewArray) {
            for (PersonInfo *person in arr) {
                NSRange foundObj=[person.truename rangeOfString:str options:NSCaseInsensitiveSearch];
                if(foundObj.length>0) {
                    [areaSearchArray addObject:person];
                }
            }
        }
    }else{
        
        
        for (NSMutableArray *arr in tableViewArray) {
            for (PersonInfo *person in arr) {
                NSRange foundObj=[person.all_workareaname rangeOfString:str options:NSCaseInsensitiveSearch];
                if(foundObj.length>0) {
                    [areaSearchArray addObject:person];
                }
            }
        }
    }
    
    if (areaSearchArray.count<1) {
        MBProgressHUD *hud = [Config createHUD];
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
        hud.labelText = @"无搜索结果";
        [hud hide:YES afterDelay:1];
        return;
        
    }else{
        
        //清空上次搜索结果,清空之前选中的成员
        [_chooseGroupMember removeAllObjects];
        [searchArray removeAllObjects];
        [searchSection removeAllObjects];
        currentSearchZiMu=nil;
        searchSectionCount=-1;
        
        for (PersonInfo *person in areaSearchArray) {
            
            //判断首字母和当前区字母是否一致
            if ([person.group isEqualToString:currentSearchZiMu]) {
                [[searchArray objectAtIndex:searchSectionCount] addObject:person];
            }else{
                
                for (int i=0; i<ziMuArray.count; i++) {
                    
                    if ([person.group isEqualToString:[ziMuArray objectAtIndex:i]]) {
                        
                        NSMutableArray *secArray=[[NSMutableArray alloc]init];
                        [secArray addObject:person];
                        [searchArray addObject:secArray];
                        searchSectionCount=searchSectionCount+1;
                        currentSearchZiMu=person.group;
                        [searchSection addObject:person.group];
                        break;
                    }
                }
                
            }
            
        }
        isSearch=YES;
        //[_chooseGroupMember removeAllObjects];
        [self.tongXunLuTableView reloadData];
    }
}

//搜索框值变化时执行
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    
    if ([searchBar.text isEqualToString:@""]) {
        isSearch=NO;
        currentSearchZiMu=nil;
        [_chooseGroupMember removeAllObjects];
        [self.view endEditing:YES];
        [self.tongXunLuTableView reloadData];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (mlmBox.isOpen) {
        [mlmBox tapAction];
    }
    [self.view endEditing:YES];
}

- (void)showAlertWithMessage:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)getTongXunLuInfo{
    
    NSArray *listArray=[[NSUserDefaults standardUserDefaults]objectForKey:@"tongXunLuArray"];
    
    if (listArray.count==0) {
        
        MBProgressHUD *hud = [Config createHUD];
        hud.mode = MBProgressHUDModeCustomView;
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
        hud.labelText = @"没有更多数据";
        [hud hide:YES afterDelay:1];
        return ;
    }
    //添加群成员
    if (self.orAddGroupMember) {
        [self setGroupMembersWithArray:listArray];
        return;
    }
    //创建群组时添加群成员
    for (NSDictionary *dic in listArray) {
        
        //PersonInfo *person=[[PersonInfo alloc]initWithDictionary:dic];
        PersonInfo *person=[PersonInfo yy_modelWithDictionary:dic];
        if ([person.group isEqualToString:currentZiMu]) {
            [[tableViewArray objectAtIndex:sectionCount] addObject:person];
        }else{
            
            for (int i=0; i<ziMuArray.count; i++) {
                
                if ([person.group isEqualToString:[ziMuArray objectAtIndex:i]]) {
                    
                    NSMutableArray *secArray=[[NSMutableArray alloc]init];
                    [secArray addObject:person];
                    [tableViewArray addObject:secArray];
                    sectionCount=sectionCount+1;
                    currentZiMu=person.group;
                    //存放区头的数组
                    [sectionArray addObject:person.group];
                    break;
                }
            }
            
        }
        //搜索数组
        //[tongXunLuArray addObject:person];
    }
    //重新刷新表
    [self.tongXunLuTableView reloadData];
}
//可选择添加的成员数据（不在群里）
-(void)setGroupMembersWithArray:(NSArray *)array{
    
    for (NSDictionary *dic in array) {
        
        //PersonInfo *person=[[PersonInfo alloc]initWithDictionary:dic];
        PersonInfo *person=[PersonInfo yy_modelWithDictionary:dic];
        BOOL orFamilly=NO;
        //判断是否已在群成员列表
        for (PersonInfo *per in self.groupArray) {
            if ([per.huanxin_username isEqualToString:person.huanxin_username]) {
                
                orFamilly=YES;
                break;
            }
        }
        
        if (orFamilly) {
            continue;
        }
        
        if ([person.group isEqualToString:currentZiMu]) {
            [[tableViewArray objectAtIndex:sectionCount] addObject:person];
        }else{
            
            for (int i=0; i<ziMuArray.count; i++) {
                
                if ([person.group isEqualToString:[ziMuArray objectAtIndex:i]]) {
                    
                    NSMutableArray *secArray=[[NSMutableArray alloc]init];
                    [secArray addObject:person];
                    [tableViewArray addObject:secArray];
                    sectionCount=sectionCount+1;
                    currentZiMu=person.group;
                    [sectionArray addObject:person.group];
                    break;
                }
            }
            
        }
        //[tongXunLuArray addObject:person];
    }
    [self.tongXunLuTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (isSearch) {
        return [[searchArray objectAtIndex:section] count];
    }
    return [[tableViewArray objectAtIndex:section] count];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if (isSearch) {
        return searchArray.count;
    }
    
    return tableViewArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    PhoneNameTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"name"];
    //
    //    if(!cell){
    //
    //        cell = [[[NSBundle mainBundle] loadNibNamed:@"PhoneNameTableViewCell" owner:nil options:nil] lastObject];
    //    }
    
    static NSString *cellIdentifier=@"name";
    BOOL nibsRegistered=NO;
    if (!nibsRegistered) {
        UINib *nib=[UINib nibWithNibName:@"PhoneNameTableViewCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:cellIdentifier];
        nibsRegistered=YES;
    }
    PhoneNameTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (isSearch) {
        cellPerson=[[searchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }else{
        cellPerson=[[tableViewArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }
    cell.phoneImg.image=[UIImage imageNamed:@"default_touxiang"];
    cell.phoneImg.layer.cornerRadius = 20;
    cell.phoneImg.clipsToBounds = YES;
    cell.phoneNameLab.text=cellPerson.truename;
    cell.workArealabel.text=cellPerson.workareaname;
    return cell;
}

- (void)loadImagesForOnscreenRows
{
    
    //获取tableview正在window上显示的cell，加载这些cell上图像。通过indexPath可以获取该行上需要展示的cell对象
    NSArray * visibleCells = [self.tongXunLuTableView indexPathsForVisibleRows];
    for (NSIndexPath * indexPath in visibleCells) {
        if (isSearch) {
            cellPerson=[[searchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        }else{
            cellPerson=[[tableViewArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
        }
        NSRange foundObj=[cellPerson.pic rangeOfString:@"default_touxiao.png" options:NSCaseInsensitiveSearch];
        if(!foundObj.length>0) {
            PhoneNameTableViewCell * cell = (PhoneNameTableViewCell *)[self.tongXunLuTableView cellForRowAtIndexPath:indexPath];
            NSLog(@"cell.phoneImgurl>>%@",cellPerson.pic);
            [cell.phoneImg setImageWithURL:[NSURL URLWithString:cellPerson.pic]];
        }
        
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    //如果tableview停止滚动，开始加载图像
    [self loadImagesForOnscreenRows];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.view endEditing:YES];
    if (mlmBox.isOpen) {
        [mlmBox tapAction];
    }
    PersonInfo *person;
    if (isSearch) {
        person=[[searchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }else{
        person=[[tableViewArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }
    //判断是否是从光荣榜进来的
    if (self.orHonorName) {
        NSArray *arr=self.navigationController.viewControllers;
        //获取数组中倒数第二个控制器
        HonorManagementViewController *honorVC= [arr objectAtIndex:[arr count]-2];
        honorVC.nameString=person.truename;
        honorVC.honorUserid=[NSString stringWithFormat:@"%d",person.userid];
        //出栈到光荣榜界面
        [self.navigationController popViewControllerAnimated:YES];
        return;
        
    }else if (self.orAddGroupMember||self.orCreateGroupMember){
        //被选中的存入数组
        [_chooseGroupMember addObject:person.huanxin_username];
        NSLog(@"选择%@",person.truename);
        return;
    }
    //当前为通讯录界面，点击进入个人详情
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    PeopleDataViewController *peopleVC=[[PeopleDataViewController alloc]init];
    peopleVC.touserid=[NSString stringWithFormat:@"%d",person.userid];
    [self.navigationController pushViewController:peopleVC animated:YES];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

//取消选择
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PersonInfo *person;
    if (isSearch) {
        person=[[searchArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }else{
        person=[[tableViewArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    }
    if ([_chooseGroupMember containsObject:person.huanxin_username]) {
        NSLog(@"取消选择%@",person.truename);
        //从保存数组里移除该人
        [_chooseGroupMember removeObject:person.huanxin_username];
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    if (isSearch) {
        return [searchSection objectAtIndex:section];
    }
    return [sectionArray objectAtIndex:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 25;
}
//确定添加群成员按钮事件
-(void)doneAction{
    
    if (self.chooseGroupMember.count<1) {
        
        [self showAlertWithMessage:@"请选择联系人!"];
    }else{
        
        NSArray *arr=self.navigationController.viewControllers;
        //获取数组中倒数第二个控制器
        if (self.orAddGroupMember) {
            //出栈到群简介
            ChatGroupDetailViewController *detailVC=[arr objectAtIndex:[arr count]-2];
            detailVC.isAddGroupMember=YES;
            detailVC.addmemberArray=self.chooseGroupMember;
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            //出栈到新建群
            CreateGroupViewController *createVC= [arr objectAtIndex:[arr count]-2];
            createVC.memberArray=self.chooseGroupMember;
            createVC.orCreateGroup=YES;
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
    //[[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
    [[SDImageCache sharedImageCache] clearMemory];
}

#pragma mark -获取通讯录分组

-(void)getAddressBookGroup{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"addressbook_group.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        if ([[result objectForKey:@"response"] isEqualToString:@"addressbook_group"]) {
            
            addressBookArray=[[NSMutableArray alloc]init];
            NSMutableArray *addressGroupArray=[[result objectForKey:@"data"] objectForKey:@"grouplist"];
            
            if (addressGroupArray.count>0) {
                
                for (NSDictionary *dic in addressGroupArray) {
                    //AddressBookGroup *addressName=[[AddressBookGroup alloc]initWithDictionary:dic];
                    AddressBookGroup *addressName=[AddressBookGroup yy_modelWithDictionary:dic];
                    [addressBookArray addObject:addressName];
                }
            }
            self.tongXunLuTableView.tableHeaderView=[self tableHeaderView];
        }
    }failureAction:^{
        
    }];
    
}

@end
